package com.func;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.Execute;
import com.Server;

/**
 * @author yinmage
 * @date 2022/08/03
 */
public abstract class Request {
    private int num = 0;
    private final List<Server> serverList;

    private final Map<Server,Integer> map;

    public Request() {
        serverList= Execute.getServerList();
        map=new HashMap<>();
        for (Server server : serverList) {
            map.put(server, 0);
        }
        System.out.println("===============================");
        System.out.println("初始化服务器列表");
        System.out.println("===============================");
        serverList.forEach(server -> System.out.printf("名称:%s: 权重:%s%n", server.getName(), server.getWeight()));
        System.out.println("===============================");
    }

    List<Server> getList() {
        return serverList;
    }

    int getNum() {
        return num;
    }

    abstract Server getServer();

    public void doRequest() {
        num++;
        Server server = getServer();
        map.put(server, map.get(server) + 1);
        System.out.printf("第%s次请求 服务器: %s%n \n", getNum(), server.getName());

    }

    public void total() {
        System.out.println("===============================");
        System.out.println("统计");
        System.out.printf("请求总数: %s%n", num);
        serverList.forEach(server -> System.out.printf("服务器: %s, 请求次数: %s%n", server.getName(), map.get(server)));
        System.out.println("===============================");
    }
}
